package com.wanxi.servlet;


import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class MyFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
//        哪些页面是需要判断用户必须登录？
//        哪些页面不需要判断用户登录?
//怎么得到访问地址？判断访问地址即可
        String path = req.getServletPath();
//        if (path.equals("/manage.jsp")) {
        if (path.startsWith("/back/")) {
            String user = (String) req.getSession().getAttribute("username");
            if (user != null) {
                chain.doFilter(req, res);
            } else {
                res.sendRedirect("/login.jsp");
            }
        } else {
            chain.doFilter(req, res);
        }

    }
}
